<template>
  <el-dialog :title="editingSurvey ? '编辑问卷' : '创建新问卷'" :visible.sync="visible" width="80%">
    <el-form ref="form" :model="survey" :rules="rules" label-width="120px">
      <el-form-item label="问卷名称" prop="name">
        <el-input v-model="survey.name"></el-input>
      </el-form-item>
      <el-form-item label="描述" prop="description">
        <el-input type="textarea" v-model="survey.description"></el-input>
      </el-form-item>
      <el-form-item label="题目列表">
        <div v-for="(question, index) in survey.questions" :key="index" class="question-container">
          <el-form-item :prop="'questions.' + index + '.type'" :rules="rules.type">
            <el-select v-model="question.type" placeholder="选择题型">
              <el-option label="单选题" value="single-choice"></el-option>
              <el-option label="多选题" value="multiple-choice"></el-option>
              <el-option label="填空题" value="fill-in-the-blank"></el-option>
              <el-option label="排序题" value="ranking"></el-option>
              <el-option label="评分题" value="rating"></el-option>
              <el-option label="下拉题" value="dropdown"></el-option>
              <el-option label="时间题" value="date-time"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item v-if="question.type !== 'ranking' && question.type !== 'date-time'" :prop="'questions.' + index + '.text'" :rules="rules.text">
            <el-input v-model="question.text" placeholder="题目文本"></el-input>
          </el-form-item>
          <el-form-item v-if="question.type === 'date-time'" :prop="'questions.' + index + '.text'" :rules="rules.date">
            <el-date-picker
              v-model="question.text"
              type="datetime"
              placeholder="选择日期时间"
            ></el-date-picker>
          </el-form-item>
          <div v-if="question.type === 'ranking'">
            <el-form-item :prop="'questions.' + index + '.text'" :rules="rules.text">
              <el-input v-model="question.text" placeholder="题目文本"></el-input>
            </el-form-item>
            <el-form-item v-for="(option, idx) in question.options" :key="idx" :prop="'questions.' + index + '.options.' + idx" :rules="rules.option">
              <el-input
                v-model="question.options[idx]"
                placeholder="选项"
                style="margin-top: 10px;"
              ></el-input>
            </el-form-item>
            <el-button type="text" @click="addOption(question)">添加选项</el-button>
          </div>
          <div v-else>
            <el-form-item v-for="(option, idx) in question.options" :key="idx" :prop="'questions.' + index + '.options.' + idx" :rules="rules.option">
              <el-input
                v-model="question.options[idx]"
                placeholder="选项"
                style="margin-top: 10px;"
              ></el-input>
            </el-form-item>
            <el-button type="text" @click="addOption(question)">添加选项</el-button>
          </div>
          <el-button type="danger" icon="el-icon-delete" circle @click="removeQuestion(index)"></el-button>
        </div>
        <el-button type="primary" @click="addQuestion">添加题目</el-button>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="closeDialog">取消</el-button>
      <el-button type="primary" @click="saveSurvey">保存</el-button>
    </span>
  </el-dialog>
</template>

<script>
export default {
  data() {
    return {
      visible: false,
      editingSurvey: null,
      survey: {
        name: '',
        description: '',
        questions: []
      },
      rules: {
        name: [
          { required: true, message: '请输入问卷名称', trigger: 'blur' }
        ],
        description: [
          { required: true, message: '请输入问卷描述', trigger: 'blur' }
        ],
        type: [
          { required: true, message: '请选择题型', trigger: 'change' }
        ],
        text: [
          { required: true, message: '请输入题目文本', trigger: 'blur' }
        ],
        date: [
          { required: true, message: '请选择日期时间', trigger: 'change' }
        ],
        option: [
          { required: true, message: '请输入选项', trigger: 'blur' }
        ]
      }
    };
  },
  methods: {
    open(survey = null) {
      this.editingSurvey = survey;
      if (survey) {
        this.survey = { ...survey };
      } else {
        this.survey = {
          name: '',
          description: '',
          questions: []
        };
      }
      this.visible = true;
    },
    closeDialog() {
      this.visible = false;
      // 清除表单验证状态
      this.$refs.form.clearValidate();
    },
    addQuestion() {
      this.survey.questions.push({
        type: 'single-choice',
        text: '',
        options: ['']
      });
    },
    removeQuestion(index) {
      this.survey.questions.splice(index, 1);
    },
    addOption(question) {
      question.options.push('');
    },
    saveSurvey() {
      this.$refs.form.validate((valid) => {
        if (valid) {
          if (this.editingSurvey) {
            this.$emit('updated', this.survey);
          } else {
            this.$emit('created', this.survey);
          }
          this.closeDialog();
        } else {
          console.log('表单验证失败');
          return false;
        }
      });
    }
  }
};
</script>

<style scoped>
.question-container {
  margin-bottom: 20px;
}
</style>
